package com.inplaceedit.client.ui.phone;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.Widget;
import com.core.gwt.ui.client.IsFocusable;
import com.inplaceedit.shared.dto.PhoneDto;

public class PhoneEditorView extends Composite implements PhoneEditor,
		IsFocusable {

	interface Binder extends UiBinder<Widget, PhoneEditorView> {
	}

	private static Binder uiBinder = GWT.create(Binder.class);

	@UiField
	TextBox number;
	@UiField(provided = true)
	ValueListBox<PhoneDto.Type> type;

	public PhoneEditorView() {
		type = new ValueListBox<PhoneDto.Type>(PhoneTypeRenderer.get());

		initWidget(uiBinder.createAndBindUi(this));

		final List<PhoneDto.Type> typeValues = new ArrayList<PhoneDto.Type>();
		for (final PhoneDto.Type value : PhoneDto.Type.values()) {
			typeValues.add(value);
		}
		type.setValue(typeValues.get(0));
		type.setAcceptableValues(typeValues);
	}

	@Override
	public Focusable asFocusable() {
		return number;
	}

	@Override
	public LeafValueEditor<String> getNumberEditor() {
		return number.asEditor();
	}

	@Override
	public LeafValueEditor<PhoneDto.Type> getTypeSelector() {
		return type.asEditor();
	}

	@UiHandler("number")
	void onNumberFocus(final FocusEvent event) {
		number.selectAll();
	}
}
